package com.liweitong.minhang.controller;

import com.liweitong.minhang.entity.NoteInfo;
import com.liweitong.minhang.service.NoteInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @Author Weton Li
 * @Date 2023/3/2 01:16
 */
@Controller
public class NoteInfoController {

    @Autowired
    NoteInfoService noteInfoService;

    @RequestMapping("/FromEditorInfo")
    @ResponseBody
    public void saveFromBigTitleInfo(String htmlInfo,String bigTitleInfo){
        System.out.println(bigTitleInfo);
        NoteInfo noteInfo = new NoteInfo();
        int size = noteInfoService.list().size();
        size++;
        noteInfo.setNid(Integer.toString(size));
        noteInfo.setBigTitleInfo(bigTitleInfo);
        noteInfo.setNoteHtml(htmlInfo);

        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());

        noteInfo.setNoteCommitTime(formatter.format(date));
        boolean isSaveFromBigTitleInfo = noteInfoService.save(noteInfo);

    }


    @RequestMapping("/getNoteInfo")
    public String getNoteInfo(Model model) {
        List<NoteInfo> list = noteInfoService.list();
//        model.addAttribute("flights", list);
        Collections.reverse(list);
        model.addAttribute("noteInfos", list);
        return "list";
    }



}
